package com.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.control.CalcuControl;
import com.model.GoodStudentModel;


public class ExportForExcel extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ExportForExcel() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<GoodStudentModel> exportExcel = new ArrayList<GoodStudentModel>();
		CalcuControl cc = new CalcuControl();
		exportExcel = cc.getGoodStudent();
		String[] headers = new String[]{"姓名","学号","平均分"};
		FileOutputStream out = null ;
		ExportExcel<GoodStudentModel>  exportForExcel = new ExportExcel<GoodStudentModel>();
		try {
			out = new FileOutputStream("C:\\goodStudent.xls");
			exportForExcel.exportExcel("优秀学生名单",headers,exportExcel,  out, "yyyy-MM-dd");
			System.out.println("EXCEL导出成功!");  
			String fileName = "goodStudent".toString();
			InputStream inStream = new FileInputStream("C:/goodStudent.xls");
	//		HttpServletResponse response = ServletActionContext.getResponse();
			response.reset();
			response.setContentType("bin");
			response.addHeader("Content-Disposition", "attachment;filename="+fileName);
			byte[] b = new byte[10];
			int len;
			try {
				while((len=inStream.read(b))>0)
					response.getOutputStream().write(b, 0, len);
					inStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{  
			   if(null != out){  
				      try {  
				            out.close();  
				      } catch (IOException e) {  
				                    e.printStackTrace();  
				      }  
		       }
			   
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
